﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;


namespace WindowsFormsApplication3.入库信息管理
{
    public partial class order_basedUse : Form
    {
        public order_basedUse()
        {
            InitializeComponent();            
        }
        //填充用户权限管理界面
        private void Read_DB_Fill_Grid(string table_name, DataGridView datagriview1)
        {
            // 用数据库将表格填满
            try
            {
                // 不自动增加dataGridview1 checkbox 增加行
                this.dataGridView1.AllowUserToAddRows = false;

                DataSet ds = 权限管理.permissionManaging.mySql.sql_search_database("select orderID,species,demand,number,picture,times from " + table_name);
               // Debug.WriteLine("select orderID,species,demand,number,picture,time from " + table_name);
                int colnum = ds.Tables[0].Columns.Count;         // 列数
                dataGridView1.ColumnCount = colnum + 1;              // 设置列数
                int rownum = ds.Tables[0].Rows.Count;            // 行数
                dataGridView1.RowCount = rownum;                 // 设置行数
                for (int i = 0; i < rownum; i++)
                {
                    for (int j = 0; j < colnum; j++)
                    {
                        dataGridView1[j+1, i].Value = ds.Tables[0].Rows[i][j].ToString();     // 把那一行显示出来
                        //Debug.WriteLine("%s\n",ds.Tables[0].Rows[i][j].ToString());
                    }
                }
            }
            catch
            {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        dataGridView1[j+1, i].Value = "";     // 把那一行显示出来
                    }
                }
            }
        }    

        private void materialManager_Load(object sender, EventArgs e)
        {
            //注意此表的orderID设置为自动增长
            权限管理.permissionManaging.mySql.excute_sql("CREATE table materialOrderTable (choice bit, orderID int identity(1,1) PRIMARY KEY,species nvarchar(50) ,demand nvarchar(50),number nvarchar(50),picture nvarchar(50),times nvarchar(50))"); //订单信息表
            //dataGridView1.RowCount = 5;
            Read_DB_Fill_Grid("materialOrderTable", dataGridView1);

        }

        private void button_add_Click(object sender, EventArgs e)
        {
            入库信息管理.order_add myOrder_add = new 入库信息管理.order_add();
            myOrder_add.ShowDialog();
            Read_DB_Fill_Grid("materialOrderTable", dataGridView1);
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            try
            {               
                入库信息管理.order_edit myOrder_edit = new 入库信息管理.order_edit();
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                string aa = dataGridView1[0, selected_index].Value.ToString();
                string bb = dataGridView1[1, selected_index].Value.ToString();
                myOrder_edit.temp_ID        = int.Parse(dataGridView1[1, selected_index].Value.ToString()); // ID
                myOrder_edit.temp_species   = dataGridView1[2, selected_index].Value.ToString();// species
                myOrder_edit.temp_demand    = dataGridView1[3, selected_index].Value.ToString(); // demand
                myOrder_edit.temp_number    = dataGridView1[4, selected_index].Value.ToString(); // number
                myOrder_edit.temp_time      = dataGridView1[5, selected_index].Value.ToString(); // time
                //permissionManaging.mySql.excute_sql("delete from materialOrderTable where orderID = '"
                //    + info_orderID + "'");
                myOrder_edit.ShowDialog();
                Read_DB_Fill_Grid("materialOrderTable", dataGridView1);
            }
            catch
            {
                MessageBox.Show("更改失败");
            }
        }

        private void button_del_Click(object sender, EventArgs e)
        {
            入库信息管理.order_del myOrder_del = new 入库信息管理.order_del();
            string[] temp_array = new string[dataGridView1.Rows.Count];
            try
            {              
                //遍历dataGridView1中checkBox已经有选择的
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    string _selectValue = dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();                
                    if (_selectValue == "True")
                    {
                        //如果CheckBox已选中，则在此处继续编写代码                     
                        temp_array[i] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    }                                  
                }
               
                DialogResult myDialogReslut = myOrder_del.ShowDialog();
                if (myDialogReslut == DialogResult.OK)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    { 
                        if(temp_array[i] !=null)
                        {
                            权限管理.permissionManaging.mySql.excute_sql("delete from materialOrderTable where "
                               + "orderID = '" + int.Parse(temp_array[i]) + "'");                         
                         }
                    }
                }
                Read_DB_Fill_Grid("materialOrderTable", dataGridView1);

            }
            catch
            { 
            
            }
        }

        

    }
}
